<?php

namespace App\Http\Utils\Integral;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * Class Order
 * @package App\Http\Utils\Integral
 */
class Order
{
    use UserContainer, ShopContainer;

    /**
     * 类型 线下shade 线上line
     * @var string
     */
    public $type;

    /**
     * 商户
     * @var Shop
     */
    public $shop;

    /**
     * 用户
     * @var User
     */
    public $user;

    /**
     * 订单号
     * @var string
     */
    public $orderNum;

    /**
     * 总金额
     * @var int
     */
    public $total;

    /**
     * 实际支付金额
     * @var int
     */
    public $amount;

    /**
     * 使用积分
     * @var int
     */
    public $useIntegral;

    /**
     * 现金激励比
     * @var float
     */
    public $cashRatio;

    /**
     * 积分激励比
     * @var float
     */
    public $inteRatio;

    /**
     * 推广计划ID
     * @var array
     */
    public $popularize;

    /**
     * 身份等级ID
     * @var array
     */
    public $distribution;

    /**
     * 订单产生的积分
     * @var float
     */
    public $integral;

    /**
     * @var float
     */
    public $cashIntegral = 0.0000;

    /**
     * @var float
     */
    public $integralIntegral = 0.0000;

    /**
     * Order constructor.
     * @param string $type 消费场景【线上|线下】
     * @param string $shopId 商户ID 【】
     * @param string $userId 用户ID 【】
     * @param string $orderNum 订单号【建议16位字符串】
     * @param int $total 消费总金额 【分】
     * @param int $amount 消费实际金额【分】
     * @param int $useIntegral 使用积分 【个】
     * @param float $cashRatio 现金激励比【%】
     * @param float $inteRatio 积分激励比 【%】
     * @param array $popularize 推广计划ID数组 【】
     * @param array $distribution 高级分销身份登记数组 【】
     */
    public function __construct(
        $type,
        $shopId,
        $userId,
        $orderNum,
        $total,
        $amount,
        $useIntegral,
        $cashRatio,
        $inteRatio,
        $popularize,
        $distribution
    )
    {
        $this->orderNum = $orderNum;
        $this->type = $type;
        $this->cashRatio = $cashRatio;
        $this->inteRatio = $inteRatio;
        $this->total = $total;
        $this->amount = $amount;
        $this->useIntegral = $useIntegral;
        $this->popularize = $popularize;
        $this->distribution = $distribution;
        /**
         * 计算订单产生的积分
         */
        $this->cashIntegral = ($this->amount / 100) * $this->cashRatio;
        $this->integralIntegral = ($this->useIntegral / 100) * $this->inteRatio;
        $temp = floor($this->cashIntegral + $this->integralIntegral);
        $this->integral = $temp > 0 ? $temp : 0;
        /**
         * 商户
         */
        $this->shop = $this->shop($shopId);
        /**
         * 用户
         */
        $this->user = $this->user($userId, $shopId);
    }

    /**
     * @return int
     */
    public function money()
    {
        return $this->amount + $this->useIntegral;
    }

    /**
     *
     */
    private function record()
    {
        DB::table('orders')->insert([
            'type'         => $this->type,
            'shop_id'      => $this->shop,
            'user_id'      => $this->user,
            'order_num'    => $this->orderNum,
            'total'        => $this->total,
            'amount'       => $this->amount,
            'use_integral' => $this->useIntegral,
            'cash_ratio'   => $this->cashRatio,
            'inte_ratio'   => $this->inteRatio,
            'popularize'   => json_encode($this->popularize),
            'distribution' => json_encode($this->distribution),
            'integral'     => $this->integral,
            'create_time'  => Time::current(),
            'update_time'  => Time::current(),
        ]);
    }

    /**
     * @return array
     * @throws \Throwable
     */
    public function account()
    {
        DB::beginTransaction();
        try {
            // 发放积分激励结算
            $this->shop
                ->flow($this->orderNum, $this->user->userId, $this->integral, '积分激励结算', 0);
            // 处理任务
            $tasks = new Tasks(new Roles($this), $this);
            // 商户记录
            $tasks->order->shopRecord();
            // 用户记录
            $tasks->order->userRecord();
            $this->record();
            // 返回今天能返回的积分
            $count = $tasks->order
                ->user($this->user->userId, $this->shop->shopId)->recordAmount;
            DB::commit();
            Cache::put($this->orderNum, round($count, 4), 3);
            return [
                'cash'     => $this->cashIntegral,
                'integral' => $this->integralIntegral,
                'user'     => round($count, 4),
            ];
        } catch (\Throwable $throwable) {
            DB::rollBack();
            throw $throwable;
        }
    }
}